<?php
/**
 * DataUtil class file.
 *
 * @package   components
 * @since     1.0
 * @filesource
 */

/**
 * DataUtil component contains some static convenient functions and constant variables to
 * perform common actions.
 * 
 * for example - static function self::getGender() will return string ("Male" or "Female") based on given value (1 or 0).
 * 
 * This class and the contained functions are accessible in global scope and usually used in model classes, controller classes,
 * or view files.
 */
class DataUtil{

	/**
	 * @var string value for "Yes" in boolean value or checkboxes
	 */
	const VALUE_YES='1';
	/**
	 * @var string value for "No" in boolean value or checkboxes
	 */
	const VALUE_NO='0';
	
	/**
	 * @var string value for "Male" in boolean value or drop down list
	 */
	const VALUE_MALE='1';
	/**
	 * @var string value for "Female" in boolean value or drop down list
	 */
	const VALUE_FEMALE='0';
	

	/**
	 * Convenient method to get string representation - Yes/No - of 1/0;
	 * OR return array of options for selection.
	 * 
	 * @param mixed $val selected value - null | 1 | 0; 
	 * @param boolean $empty whether include empty options when return options array.
	 * @return Ambigous <string>|multitype:string Yes or No if $val is specified; OR array if not.
	 */
	public static function getOptionYesNo($val=NULL, $empty=FALSE){
		// Set the options depanding on the $empty value - if empty value is needed.
		$options = $empty ? array(''=>'',self::VALUE_YES=>'Yes',self::VALUE_NO=>'No'):
									array(self::VALUE_YES=>'Yes',self::VALUE_NO=>'No');
		// If $val is specified, then return string "Yes", or "No".
		if($val != null){
			return $options[$val];
		}
		// If $val is not specifed, then return options array.
		return $options;
	}

	/**
	 * Get array of Australian States. 
	 * The array is usually used to produce drop down list.
	 * @return array.  array['NSW']='NSW'
	 */
	public static function getStates(){
		return array(
			'NSW'=>'NSW',
			'ACT'=>'ACT',
			'VIC'=>'VIC',
			'QLD'=>'QLD',
			'SA'=>'SA',
			'WA'=>'WA',
			'TAS'=>'TAS',
			'NT'=>'NT',
		);
	}

	/**
	 * Get array of personal titles.
	 * The array is usually used to produce drop down list.
	 * @return array of string array['Mr']='Mr'
	 */
	public static function getTitles(){
		return array(
			'Mr'=>'Mr',
			'Ms'=>'Ms',
			'Miss'=>'Miss',
			'Mrs'=>'Mrs',
			'Sir'=>'Sir',
	 		'Lady'=>'Lady',
	 		'Dr'=>'Dr',
	 		'Master'=>'Master',
	 		'Sr'=>'Sr',
		);
	}
	
	/**
	 * Convenient method to get an array from 0 to $max.
	 * @param int $max the max int value of the array
	 * @return array an array from 0 to $max. eg: $result[0]=0; $result[10]=10;
	 */
	public static function getNumers($max){
		// Set return variable - $results.
		$results=array();
		
		// populate the array via a loop
		for ($i = 0; $i <=$max; $i++) {
			$results[$i]=$i;
		}
		
		// Return results.
		return $results;
	}
	
	/**
	 * Get array of genders.
	 * Convenient method to get genders for drop down list.
	 * @return array genders in key=>value. The array is array(0=>"Female", 1=>"Male");
	 */
	public static function getGenders(){
		return array(self::VALUE_FEMALE=>"Female",self::VALUE_MALE=>"Male");
	}
	
	/**
	 * Decode a gender value into text representation. 
	 * @param mixed $gender the gender code
	 * @return string "Male" or "Female". eg: $result[1]="Female";
	 */
	public static function getGender($gender){
		// Set genders array.
		$genders = array(self::VALUE_FEMALE=>"Female",self::VALUE_MALE=>"Male");
		
		// Return the String regarding to the value 0 or 1.
		return $genders[$gender];
	}
	
	
	/**
	 * Delete a given directory and all included files and subdirectories under the given directory.
	 * 
	 * eg: DataUtil::rrmdir("C:\dir1\dir2\dir"); will delete the files and directories under dir. If there are some direcotries in dir, or
	 * directories and files under dir's child directories, they will be deleted no matter how deep they are located. If the operation is 
	 * succussful, return true, otherwise, false.
	 * 
	 * @param string $dir the path to a directory.
	 * @return boolean True|False
	 */
	public static function rrmdir($dir) {
		// Check if the target is a directory, if not, return false.
		if (!is_dir($dir))
			return false;
		
		// Get List of files and directories inside the specified path
		$objects = scandir($dir);
		
		// Check each files and direcotries inside the specifed path via a loop.
		foreach ($objects as $object) {
			
			// Ignore "." and "..", they do not need to be deleted.
			if ($object != "." && $object != "..") {
				
				// Check if the object is an directory.
				if (filetype($dir."/".$object) == "dir")
					// if the object is an directory, then call this function again 
					// to delete all files or directories inside the object.
					self::rrmdir($dir."/".$object);
				else 
					// if the object is not a direcotry but a file, then delete it.
					@unlink($dir."/".$object);
			}
		}
		
		// Reset the pointer inside array $object for safety.
		reset($objects); 
		// Delete the targeted directory.
		@rmdir($dir);
		// Return true, because all operation are finished.
		return true;
	}
}